/****************************************************************************
*                                                                           *
* material.cpp 															    *
*                                                                           *
* ALaRI 2008												                *
*                                                                           *
****************************************************************************/


/****************************************************************************
* Includes										                            *
****************************************************************************/
#include "material.h"
#include <memory>

CMaterial::~CMaterial()
{ 
	SafeFree(m_pScatMatrix); 
}

CMaterial::CMaterial(const QString& strName, unsigned int nColor, float fltA, 
					 float fltN, float fltDealtaR, float fltFreq) 
	: m_strName(strName),
	m_nColor(nColor),
	m_fltA(fltA),
	m_fltN(fltN)
{
	// ensure parameters are valid
	assert(nColor >= 0);
	assert(fltA >= 0.f && fltA <= 1.f);
	
	// get exponent part
	std::complex<float> cfltExpPart = std::exp(
		std::complex<float>(0.f, - s_fltEPOWSpec * fltDealtaR * fltFreq));
	// calculate auxiliary N2
	float fltN2 = fltN * fltN;

	// calculate thetta0
	m_cfltTh0 = (fltA / (2.f * fltN2)) * cfltExpPart;

	// complex auxiliary YK
	std::complex<float> cfltYK = ((4.f * (fltN2 - 1.f)) * m_cfltTh0) /
		(1.f - m_cfltTh0 * (2.f * fltN2 - 4.f));
	// create the matrix
	std::auto_ptr<CScatMatrix> apScatMatrix(new CScatMatrixL0(
		m_cfltTh0 * (cfltYK + 1.f), m_cfltTh0 * ((1.f - 2.f * fltN2) + cfltYK)));

	m_pScatMatrix = apScatMatrix.release();
}

std::complex<float> CMaterial::calculateKN2() const
{
	float fltN2 = m_fltN * m_fltN;
	std::complex<float> cfltKN2 = (1.f + m_cfltTh0 /
		(1.f - m_cfltTh0 * (2.f * fltN2 - 4.f))) / fltN2;
	return cfltKN2;
}
/****************************************************************************
* End of file																*
****************************************************************************/